package top.wenzz.manage.system.manage.web;

import org.springframework.web.bind.annotation.*;
import top.wenzz.commons.vo.RetMsg;
import top.wenzz.manage.system.manage.entity.SysRole;
import top.wenzz.manage.system.manage.service.SysPermissionService;
import top.wenzz.manage.system.manage.service.SysRoleService;
import top.wenzz.commons.utils.ExceptionUtils;
import top.wenzz.commons.web.BasicCrudController;

/**
 * 系统角色controller
 * Created by 钟廷员 on 2016/12/15.
 */
@RestController
@RequestMapping("sys/role")
public class SysRoleController extends BasicCrudController<SysRole, SysRoleService> {

    @RequestMapping( value = "allQuery", method = RequestMethod.POST )
    public Object allQuery() {
        return RetMsg.success(service.findAll());
    }

    @RequestMapping( value = "getRolePermission/{id}", method = RequestMethod.POST )
    public Object getRolePermission(@PathVariable int id) {
        return RetMsg.success(SysPermissionService.createTree(service.getOne(id).getPermissions()));
    }

    @RequestMapping( value = "updatePermission", method = RequestMethod.POST )
    public Object updatePermission(@RequestBody SysRole entity) {
        String ret;
        try {
            ret = service.updatePermission(entity);
        } catch (Exception e) {
            ret = ExceptionUtils.getExceptionMsg(e);
        }
        return ret(ret, "修改成功");
    }

}
